﻿using System.Collections.Generic;
using System.Linq;

namespace Sidvall.Data.Sqlite
{
    public static class DataManager
    {
        #region Public Members

        #region Execute

        public static void Execute(SQLite.Net.SQLiteConnection connection, SqlQuery sqlQuery)
        {
            if (connection == null)
                return;
            if (sqlQuery == null)
                return;
            connection.Execute(sqlQuery.Sql.ToString(), sqlQuery.ParameterValues.ToArray());
        }
        public static void Execute(SQLite.Net.SQLiteConnection connection, SqlQueryCollection sqlQueries)
        {
            if (connection == null)
                return;
            if (sqlQueries == null)
                return;
            if (sqlQueries.UseTransaction)
            {
                foreach (var query in sqlQueries)
                    connection.Execute(query.Sql.ToString(), query.ParameterValues.ToArray());
            }
            else
            {
                foreach (var query in sqlQueries)
                    connection.Execute(query.Sql.ToString(), query.ParameterValues.ToArray());
            }
        }

        #endregion
        #region ExecuteScalar

        public static T ExecuteScalar<T>(SQLite.Net.SQLiteConnection connection, SqlQuery sqlQuery)
        {
            if (connection == null)
                return default(T);
            if (sqlQuery == null)
                return default(T);
            return connection.ExecuteScalar<T>(sqlQuery.Sql.ToString(), sqlQuery.ParameterValues.ToArray());
        }

        #endregion
        #region Query

        public static List<T> Query<T>(SQLite.Net.SQLiteConnection connection, SqlQuery sqlQuery) where T : class
        {
            if (connection == null)
                return null;
            if (sqlQuery == null)
                return null;
            return connection.Query<T>(sqlQuery.Sql.ToString(), sqlQuery.ParameterValues.ToArray());
        }

        #endregion

        #endregion
    }
}
